// packagePay/pages/new-create-project/new-create-project.js
import {
    getBindCompanySupplier,
    createDayProject,
    createMonthProject
} from '../../../api/app'
import user from '../../../utils/user'
import {
    dateFormat,
    toast
} from '../../../utils/util'
const app = getApp()
Page({
    data: {
        projectName: '', // 项目名称
        projectType: 0,
        signInMode: 0, // 切换白夜班
        checkInMode: 0, // 1定位 0拍照 
        companyList: [], // 暂存
        tipList: false,
        enterpriseTipId: '',
        enterpriseTipName: '',
        date: '',
        timeList: [],
        startTime: '',
        endTime: '',
        deposit: '', // 押薪
        ageArr: [], // 年龄
        ageGirlArr: [],
        ageSectionMan: '不限制-不限制',
        address: '',
        describes: '',
        hardwareId: [],
        male: false,
        female: false,
        ageSectionWomen: '不限制-不限制',
        noLimit: true,
        noLimitGirl: false,
        noLimitMan: false
    },
    onProjectNameChange(e) {
        console.log(e.detail.value, 'uyhsgduyhidsgb')
        // eslint-disable-next-line no-misleading-character-class
        let reg = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig
        if (reg.test(e.detail.value)) {
            wx.showToast({
                title: '不可输入表情',
                icon: 'none'
            })
            this.setData({
                projectName: this.data.projectName
            })
        } else {
            this.setData({
                projectName: e.detail.value
            })
        }
    },
    async onLoad(options) {
        await user.waitLogin()
        const timeList = [
            ['全天'],
            []
        ]
        const ageArr = [
            ['不限制'],
            ['不限制']
        ]
        const ageGirlArr = [
            ['不限制'],
            ['不限制']
        ]
        for (let i = 14; i <= 80; i++) {
            ageArr[0].push(i)
            ageGirlArr[0].push(i)
        }
        ageArr[1] = ageArr[0]

        // for(let i=14;i<=80;i++){
        //     ageGirlArr[0].push(i)
        // } 
        ageGirlArr[1] = ageGirlArr[0]
        // ageArr[2] = ageArr[0]
        // ageArr[3] = ageArr[0]
        for (let i = 0; i < 24; i++) {
            timeList[0].push(i < 10 ? '0' + i : i)
        }
        for (let k = 0; k < 60; k++) {
            timeList[1].push(k < 10 ? '0' + k : k)
        }
        console.log(timeList, 'timeList')
        this.setData({
            date: dateFormat('YYYY-mm-dd', new Date()),
            timeList,
            ageArr,
            ageGirlArr,
            checkInMode: user.signInMode
        })
    },
    onReady() {

    },
    onShow() {
        user.waitLogin().then(res => {
            this.getBindCompanySupplier()
        })
    },
    onSetProjectType(event) {
        const {
            index
        } = event.currentTarget.dataset
        console.log(event, 'event')
        this.setData({
            projectType: index
        })
    },
    // 拿到所有企业
    getBindCompanySupplier() {
        getBindCompanySupplier({
            companyId: user.companyId,
            type: 0
        }).then(res => {
            console.log(res, 'resres')
            this.setData({
                companyList: res
            })
        })
    },
    // 切换白夜班
    onChangeClockMode(event) {
        console.log(event, 'event')
        this.setData({
            signInMode: event.detail.value
        })
    },
    // 显示选择企业下拉列表
    onEnterInput() {
        console.log('点击啦吗')
        this.setData({
            tipList: this.data.companyList
        })
    },
    onInput(event) {
        const data = []
        console.log(this.data.companyList, 'event021202110')
        for (const i in this.data.companyList) {
            if (this.data.companyList[i].serveCompanyName && this.data.companyList[i].serveCompanyName.indexOf(event.detail.value) !== -1) {
                data.push(this.data.companyList[i])
            }
        }
        this.setData({
            'enter': event.detail.value,
            'enterId': null,
            tipList: data
        })
    },
    onMove(event) {
        console.log(event, '触发到啦吗')
        this.setData({
            tipList: false
        })
    },
    // 选择企业
    tapEnter(event) {
        console.log(event, 'event')
        if (event.currentTarget.dataset.isdisabled) {
            wx.showToast({
                title: '已禁用',
                icon: 'none'
            })
            return
        }
        const {
            id,
            name
        } = event.currentTarget.dataset
        this.setData({
            enterpriseTipId: id,
            enterpriseTipName: name,
            tipList: false
        })
    },
    // 点击进入企业管理页面
    onGoEnterprise() {
        wx.navigateTo({
            url: '/pages/enterprise/enterprise',
        })
    },
    // 选择日期
    onSetDate(event) {
        this.setData({
            date: event.detail.value
        })
    },
    // 前往选择打卡地点
    onSelectAddress() {
        wx.navigateTo({
            url: '/packagePay/pages/clock-address/clock-address'
        })
    },
    onHardWare() {
        const t = this
        wx.navigateTo({
            url: '/packagePay/pages/device-manage/device-manage?isSelect=1',
            events: {
                acceptDataFromOpenedPage: (data) => {
                    console.log(data, 'datadata')
                    t.data.hardwareId = data.data.map(item => {
                        return item.id
                    })
                    t.setData({
                        isDeviceClock: t.data.hardwareId.length ? '已开启硬件打卡' : '',
                        hardwareId: t.data.hardwareId
                    });
                    (res) => {
                        res.eventChannel.emit('acceptDataFromOpenerPage', {
                            data: t.data.hardwareId
                        })
                    }
                }
            }
        })
    },
    // 接受clock-address的调用
    selectAddressComplete(data) {
        console.log(data)
        this.setData({
            address: data,
            describes: data.current.describe
        })
    },
    // 选择开始时间
    onSetStartTime(event) {
        const {
            value
        } = event.detail
        const {
            timeList
        } = this.data
        if (value[0] == 0) {
            this.setData({
                startTime: '00:00'
            })
            return
        }
        this.setData({
            startTime: timeList[0][value[0]] + ':' + timeList[1][value[1]]
        })
    },
    // 选择结束时间
    onSetEndTime(event) {
        console.log(event, 'event')
        const {
            value
        } = event.detail
        const {
            timeList
        } = this.data
        if (value[0] == 0) {
            this.setData({
                endTime: '00:00'
            })
            return
        }
        this.setData({
            endTime: timeList[0][value[0]] + ':' + timeList[1][value[1]]
        })
    },
    onSetGirlAgeCurrent(event) {
        this.setData({
            male: true,
            female: true
        })
        const {
            value
        } = event.detail
        const {
            ageGirlArr
        } = this.data
        console.log(event, 'event')
        if (value[0] !== 0 && value[1] !== 0) {
            if (value[0] > value[1]) {
                app.toast('女性起始年龄不可大于结束年龄')
                return
            }
            if (value[1] < value[0]) {
                app.toast('女性结束年龄不可小于开始年龄')
                return
            }
        }

        console.log(this.data.ageArr[0][0], '这个第0项是什么', this.data.ageArr, '第一项是什么')
        console.log(event.detail.value == 0, '子项这个第0项是什么', this.data.ageArr, '第一项是什么')
        //  && this.data.ageArr[0][0] == '不限制' && this.data.ageArr[1][0] == '不限制'
        if (event.detail.value[0] == 0 && event.detail.value[1] == 0) {
            this.setData({
                noLimitGirl: true
            }, () => {
                console.log(this.data.noLimitGirl, '1girl的值是什么', this.data.noLimitMan, '1Man的值是什么')
                if (this.data.noLimitGirl === true && this.data.noLimitMan === true) {
                    this.setData({
                        noLimit: true,
                        noLimitGirl: false,
                        noLimitMan: false
                    })
                } else {
                    this.setData({
                        noLimit: false
                    })
                }
            })
        } else {
            this.setData({
                noLimitGirl: false
            }, () => {
                console.log(this.data.noLimitMan, '2Man的值是什么', this.data.noLimitMan, '2Man的值是什么')
                if (this.data.noLimitGirl === true && this.data.noLimitMan === true) {
                    this.setData({
                        noLimit: true,
                        noLimitGirl: false,
                        noLimitMan: false
                    })
                } else {
                    this.setData({
                        noLimit: false
                    })
                }
            })
        }
        this.setData({
            ageSectionWomen: value[0] == 0 ? '不限制' + '-' + ageGirlArr[1][value[1]] : value[1] == 0 ? ageGirlArr[0][value[0]] + '-' + '不限制' : (!value[0] && !value[1]) ? '不限制-不限制' : ageGirlArr[0][value[0]] + '-' + ageGirlArr[1][value[1]],
            ageUnit: value[1] == 0 ? false : true,
        })

    },
    // 选择年龄限制
    onSetAgeCurrent(event) {
        console.log(event, '年龄限制是什么')
        this.setData({
            male: true,
            female: true
        })
        const {
            value
        } = event.detail
        if (value[0] !== 0 && value[1] !== 0) {
            if (value[0] > value[1]) {
                app.toast('男性起始年龄不可大于结束年龄')
                return
            }
            if (value[1] < value[0]) {
                app.toast('男性结束年龄不可小于开始年龄')
                return
            }
        }
        const {
            ageArr
        } = this.data
        console.log(event, 'event')
        console.log(this.data.ageGirlArr[0][0], '这个数组是什么')
        //  && this.data.ageGirlArr[0][0] == '不限制' && this.data.ageGirlArr[1][0] == '不限制'
        if (event.detail.value[0] == 0 && event.detail.value[1] == 0) {
            this.setData({
                noLimitMan: true,
            }, () => {
                console.log(this.data.noLimitGirl, '3girl的值是什么', this.data.noLimitMan, '3Man的值是什么')
                if (this.data.noLimitGirl === true && this.data.noLimitMan === true) {
                    this.setData({
                        noLimit: true
                    })
                } else {
                    this.setData({
                        noLimit: false
                    })
                }
            })
        } else {
            console.log(event.detail.value[0], '11111111111', event.detail.value[1])
            this.setData({
                noLimitMan: false
            }, () => {
                console.log(this.data.noLimitGirl, '4girl的值是什么', this.data.noLimitMan, '4Man的值是什么')
                if (this.data.noLimitGirl === true && this.data.noLimitMan === true) {
                    this.setData({
                        noLimit: true
                    })
                } else {
                    this.setData({
                        noLimit: false
                    })
                }
            })
        }
        // 显示在页面的内容
        this.setData({
            ageSectionMan: value[0] == 0 ? '不限制' + '-' + ageArr[1][value[1]] : value[1] == 0 ? ageArr[0][value[0]] + '-' + '不限制' : (!value[0] && !value[1]) ? '不限制-不限制' : ageArr[0][value[0]] + '-' + ageArr[1][value[1]],
            ageUnit: value[1] == 0 ? false : true,
        })
    },
    // 创建日 createDayProject
    async onCreateDayProject() {
        this.data.projectName = this.data.projectName.replace(/\s/g, '')
        if (/\s+/.test(this.data.projectName)) {
            toast('不可输入多个空格')
            return
        }
        if (/\.\./.test(this.data.projectName)) {
            toast('项目名称不规范，不可输入多个.')
            return
        }
        if (!this.data.projectName) {
            toast('请输入项目名称')
            return
        }
        if (!this.data.enterpriseTipName) {
            toast('请选择企业')
            return
        }
        await createDayProject({
            ageRange: this.data.ageSectionMan.replace(/不限制/g, '~'),
            ageRangeFemale: this.data.ageSectionWomen.replace(/不限制/g, '~'),
            companyId: user.companyId,
            createUserId: user.userId,
            date: this.data.date,
            dayNightFlag: this.data.signInMode,
            isOpenWesure: 0,
            projectName: this.data.projectName,
            serveCompanyId: this.data.enterpriseTipId,
            serveCompanyName: this.data.enterpriseTipName,
            type: this.data.projectType,
            wesureType: null,
            wesureTypeName: null,
        }).then(res => {
            console.log(res, 'resres')
            setTimeout(() => {
                wx.redirectTo({
                    url: '/packagePay/pages/new-resident/new-resident?id=' + res
                })
            }, 500)
        })
    },
    // 创建月 createMonthProject
    async onCreateMonthProject() {
        this.data.projectName = this.data.projectName.replace(/\s/g, '')
        if (this.data.deposit && !/^\d+(\.\d+)?$/.test(this.data.deposit)) {
            toast('压薪天数只能输入数字')
            return
        }
        if (/\s+/.test(this.data.projectName)) {
            toast('不可输入多个空格')
            return
        }
        if (/\.\./.test(this.data.projectName)) {
            toast('项目名称不规范，不可输入多个.')
            return
        }
        if (!this.data.projectName) {
            toast('请输入项目名称')
            return
        }
        if (!this.data.enterpriseTipName) {
            toast('请选择企业')
            return
        }
        if (!this.data.startTime) {
            toast('请选择上班打卡时间')
            return
        }
        if (!this.data.endTime) {
            toast('请选择下班打卡时间')
            return
        }
        if (!this.data.describes && this.data.checkInMode) {
            toast('请选择打卡点')
            return
        }
        if (this.data.deposit && !/^\d+(\.\d+)?$/.test(this.data.deposit)) {
            toast('压薪天数只能输入数字')
            return
        }
        if (!this.data.deposit) {
            this.setData({
                deposit: 0
            })
        }
        console.log(this.data, 'datadata')
        createMonthProject({
            ageRange: this.data.ageSectionMan.replace(/不限制/g, '~'),
            ageRangeFemale: this.data.ageSectionWomen.replace(/不限制/g, '~'),
            companyId: user.companyId,
            createUserId: user.userId,
            date: this.data.date,
            dayNightFlag: this.data.signInMode,
            ibeaconIds: this.data.hardwareId,
            projectName: this.data.projectName,
            serveCompanyId: this.data.enterpriseTipId,
            serveCompanyName: this.data.enterpriseTipName,
            signLocation: this.data.address.longitude + ',' + this.data.address.latitude,
            signPlace: this.data.checkInMode ? this.data.address.current.name : '',
            signRange: this.data.address.radius || '',
            signTime: this.data.startTime + '-' + this.data.endTime,
            staffUnderpay: this.data.deposit,
            type: this.data.projectType,
        }).then(res => {
            setTimeout(() => {
                wx.redirectTo({
                    url: '/packagePay/pages/new-resident/new-resident?id=' + res
                })
            }, 500)
        })
    }
})